import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';

import 'logic.dart';

class NoticeEditPage extends StatelessWidget {
  NoticeEditPage({Key? key}) : super(key: key);

  final logic = Get.find<NoticeEditLogic>();

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back_ios,
              size: 16.w,
            ),
            onPressed: () {
              Get.back();
            },
          ),
          title: logic.isCreate.value
              ? const Text('发布案源公告')
              : const Text('编辑案源公告'),
        ),
        body: Form(
          key: logic.formKey,
          child: ListView(
            children: [
              SizedBox(height: kSpaceMedium),

              Container(
                margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
                padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(kRadiusMedium),
                  color: Colors.white,
                ),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '当事人联系方式',
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            color: AppColor.colorFontBlack,
                          ),
                        ),
                        // 联系电话输入框
                        Expanded(
                          child: TextFormField(
                            textAlign: TextAlign.right,
                            controller: logic.phoneController,
                            decoration: InputDecoration(
                              hintText: '请输入',
                              hintStyle: TextStyle(
                                fontSize: kFontSizeSmall,
                                color: AppColor.colorFontGrey,
                              ),
                              border: InputBorder.none,
                            ),
                            // validator: (value) {
                            //   if (value == null || value.isEmpty) {
                            //     return '请输入当事人联系方式';
                            //   }
                            //   // 输入内容为手机号码验证
                            //   if (!RegExp(r'^1[3456789]\d{9}$')
                            //       .hasMatch(value)) {
                            //     return '请输入正确的手机号码';
                            //   }
                            //   return null;
                            // },
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: kSpaceTiny),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '是否展示发布人联系方式',
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            color: AppColor.colorFontBlack,
                          ),
                        ),
                        Switch(
                          value: logic.ifShowOwnerPhone.value,
                          onChanged: (value) => logic.setShowOwnerPhone(value),
                        ),
                      ],
                    ),
                  ],
                ),
              ),

              SizedBox(height: kSpaceMedium),

              Container(
                margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
                padding: EdgeInsets.symmetric(
                    horizontal: kSpaceMedium, vertical: kSpaceMedium),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(kRadiusMedium),
                  color: Colors.white,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '发布渠道',
                      style: TextStyle(
                        fontSize: kFontSizeMedium,
                        color: AppColor.colorFontBlack,
                      ),
                    ),
                    SizedBox(height: kSpaceMedium),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Expanded(
                          child: _buildSelectionBtn(
                              '机构内部', 1, logic.shareType.value == 1),
                        ),
                        SizedBox(width: kSpaceSmall),
                        Expanded(
                          child: _buildSelectionBtn(
                              '跨机构公开', 2, logic.shareType.value == 2),
                        ),
                      ],
                    ),
                    SizedBox(height: kSpaceMedium),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '标题',
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            fontWeight: AppFont.fontRegular,
                            color: AppColor.colorFontBlack,
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            textAlign: TextAlign.right,
                            controller: logic.titleController,
                            decoration: InputDecoration(
                              hintText: '详情摘要或案源分类等，可选',
                              hintStyle: TextStyle(
                                fontSize: kFontSizeSmall,
                                color: AppColor.colorFontLabelGrey,
                              ),
                              contentPadding: EdgeInsets.only(
                                  top: kSpaceSmall, bottom: kSpaceMini),
                              border: InputBorder.none,
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: kSpaceTiny),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '标的额',
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            fontWeight: AppFont.fontRegular,
                            color: AppColor.colorFontBlack,
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            textAlign: TextAlign.right,
                            controller: logic.subjectAmountController,
                            decoration: InputDecoration(
                              hintText: '可以是区间，和服务价格必填其一',
                              hintStyle: TextStyle(
                                fontSize: kFontSizeSmall,
                                color: AppColor.colorFontLabelGrey,
                              ),
                              contentPadding: EdgeInsets.only(
                                  top: kSpaceSmall, bottom: kSpaceMini),
                              border: InputBorder.none,
                            ),
                            keyboardType: TextInputType.number,
                            validator: (value) {
                              if ((value == null || value.isEmpty) &&
                                  logic.servicePriceController.text.isEmpty) {
                                return '请输入标的额或者服务价格';
                              }
                              return null;
                            },
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: kSpaceTiny),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '服务价格',
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            fontWeight: AppFont.fontRegular,
                            color: AppColor.colorFontBlack,
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            textAlign: TextAlign.right,
                            controller: logic.servicePriceController,
                            decoration: InputDecoration(
                              hintText: '可以是区间，和标的额必填其一',
                              hintStyle: TextStyle(
                                fontSize: kFontSizeSmall,
                                color: AppColor.colorFontLabelGrey,
                              ),
                              contentPadding: EdgeInsets.only(
                                  top: kSpaceSmall, bottom: kSpaceMini),
                              border: InputBorder.none,
                            ),
                            keyboardType: TextInputType.number,
                            validator: (value) {
                              if ((value == null || value.isEmpty) &&
                                  logic.subjectAmountController.text.isEmpty) {
                                return '请输入服务价格或者标的额';
                              }
                              return null;
                            },
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: kSpaceTiny),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '案源比例',
                          style: TextStyle(
                            fontSize: kFontSizeSmall,
                            fontWeight: AppFont.fontRegular,
                            color: AppColor.colorFontBlack,
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            textAlign: TextAlign.right,
                            controller: logic.cooperRatioController,
                            decoration: InputDecoration(
                              suffixText: '%',
                              suffixStyle: TextStyle(
                                fontSize: kFontSizeSmall,
                                color: AppColor.colorFontBlack,
                              ),
                              hintText: '请输入',
                              hintStyle: TextStyle(
                                fontSize: kFontSizeSmall,
                                color: AppColor.colorFontLabelGrey,
                              ),
                              contentPadding: EdgeInsets.only(
                                  top: kSpaceSmall, bottom: kSpaceMini),
                              border: InputBorder.none,
                            ),
                            keyboardType: TextInputType.number,
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: kSpaceMini),
                    Text(
                      '详细内容',
                      style: TextStyle(
                        fontSize: kFontSizeSmall,
                        fontWeight: AppFont.fontRegular,
                        color: AppColor.colorFontBlack,
                      ),
                    ),

                    // 详细内容输入框
                    TextFormField(
                      controller: logic.contentController,
                      maxLines: 5,
                      decoration: InputDecoration(
                        alignLabelWithHint: true,
                        hintText: '请输入案情、案由或对达人的要求，尽量详细描述，以便于为您智能匹配合适人选',
                        hintStyle: TextStyle(
                          fontSize: kFontSizeSmall,
                          color: AppColor.colorFontLabelGrey,
                        ),
                        border: InputBorder.none,
                      ),
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return '请输入详细内容';
                        }
                        return null;
                      },
                    ),
                  ],
                ),
              ),

              SizedBox(height: kSpaceMedium),

              // 附件上传
              Container(
                margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
                padding: EdgeInsets.symmetric(
                  horizontal: kSpaceMedium,
                  vertical: kSpaceMedium,
                ),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(kRadiusMedium),
                  color: Colors.white,
                ),
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '案源附件',
                        style: TextStyle(
                          fontSize: kFontSizeMedium,
                          color: AppColor.colorFontBlack,
                        ),
                      ),
                      SizedBox(height: kSpaceTiny),
                      _buildUploadAttachments(),
                    ]),
              ),

              // 提交按钮
              Padding(
                  padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
                  child: _buildSubmitButton()),
              SizedBox(height: kSpaceSmall),
            ],
          ),
        ),
      ),
    );
  }

  // 构建单选按钮
  Widget _buildSelectionBtn(String title, int selectedValue, bool selected) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: selected ? AppColor.colorFontBlue : Colors.transparent,
        ),
        color: selected ? const Color(0xFFF2F3FF) : const Color(0xFFF4F6F9),
        borderRadius: BorderRadius.circular(kRadiusSmall),
      ),
      padding: EdgeInsets.symmetric(
        vertical: kSpaceMini,
        horizontal: kSpaceMedium,
      ),
      child: InkWell(
        onTap: () {
          logic.setShareType(selectedValue);
        },
        child: Text(
          title,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: selected ? AppColor.colorFontBlue : AppColor.colorFontGrey,
            fontSize: kFontSizeMedium,
          ),
        ),
      ),
    );
  }

  // 生成提交按钮小部件
  Widget _buildSubmitButton() {
    return Container(
      height: 50.w,
      margin: EdgeInsets.only(top: kSpaceMedium),
      child: FilledButton(
        onPressed: () => logic.onSubmitted(),
        child: const Text('提交'),
      ),
    );
  }

  Widget _buildUploadAttachments() {
    return Obx(
      () => Column(
        children: List.generate(
          logic.attachmentNames.length + 1,
          (index) => Container(
            margin: EdgeInsets.only(top: kSpaceTiny),
            child: FilledButton(
              onPressed: () => logic.pickFile(),
              style: ElevatedButton.styleFrom(
                foregroundColor: AppColor.colorFontBlue,
                backgroundColor: Colors.transparent,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(kRadiusSmall),
                ),
                padding: EdgeInsets.zero,
              ),
              child: Container(
                alignment: Alignment.center,
                width: double.maxFinite,
                child: index < logic.attachmentNames.length
                    ? Container(
                        padding: EdgeInsets.only(left: kSpaceMedium),
                        margin: EdgeInsets.only(top: kSpaceMini),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(kRadiusMedium),
                          color: AppColor.colorTagBlue,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Expanded(
                              child: Text(
                                logic.attachmentNames[index],
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  fontSize: kFontSizeSmall,
                                  color: AppColor.colorFontBlue,
                                  fontWeight: AppFont.fontRegular,
                                ),
                              ),
                            ),
                            IconButton(
                              onPressed: () => logic.deleteFile(index),
                              icon: const Icon(Icons.close, color: Color(0xFFC7C8DB)),
                            ),
                          ],
                        ),
                      )
                    : SvgPicture.asset(
                        'assets/buttons/ic_upload.svg',
                        width: 111.5.w,
                        height: 21.w,
                      ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
